package com.test.mytest.awt;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;

public class CreatePassword {

	protected Shell shell;
	private Text text;
	private Text text_1;

	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			CreatePassword window = new CreatePassword();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(361, 79);
		shell.setText("密码加密");
		shell.setLayout(new GridLayout(4, false));
		
		Label lblNewLabel = new Label(shell, SWT.NONE);
		lblNewLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblNewLabel.setText("密码");
		
		text = new Text(shell, SWT.BORDER | SWT.PASSWORD);
		text.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				if (e.keyCode==13) {
					update();
				}
			}
		});
		GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_text.widthHint = 230;
		text.setLayoutData(gd_text);
		
		text_1 = new Text(shell, SWT.BORDER);
		text_1.setToolTipText("");
		GridData gd_text_1 = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd_text_1.widthHint = 12;
		text_1.setLayoutData(gd_text_1);
		
		Button btnNewButton = new Button(shell, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				update();
			}
		});
		btnNewButton.setText("生成");

	}

	protected void sendPassword(String text2, Integer num) {
		String encod = RobotUtils.encod(text2);
		if (num!=null) {
			encod=encod.substring(0, num);
		}
		System.out.println(text2+"-生成密码=="+encod);
		RobotUtils.keyPressString(encod, RobotUtils.getRobot());
	}

	/**
	 * 
	 */
	private void update() {
		shell.setVisible(false);
		String txt = text.getText();
		String length = text_1.getText();
		Integer num = null;
		try {
			num = Integer.valueOf(length);
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		sendPassword(txt,num);
		text.setText("");
		shell.setVisible(true);
	}
	
	public String get(String value,String key){
		char[] vChar = value.toCharArray();
		char[] kChar = key.toCharArray();
		for (int i = 0; i < kChar.length; i++) {
			for (int j = 0; j < key.length(); j++) {
				vChar[i]^=kChar[j++/key.length()];
			}
		}
		return new String(vChar);
	}
}
